import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/config/app_config.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/widgets/button/common_button.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionUtils {
  static Future<bool> checkPermission(Permission permission,
      {Function? settingPermission}) async {
    // if (Platform.isAndroid && permission == Permission.photos) {
    //   final androidInfo = await DeviceInfoPlugin().androidInfo;
    //   if (androidInfo.version.sdkInt <= 32) {
    //     permission = Permission.storage;
    //   } else {
    //     permission = Permission.photos;
    //   }
    // }

    PermissionStatus status = await permission.request();
    if (status == PermissionStatus.denied ||
        status == PermissionStatus.permanentlyDenied) {
      String infoStr = '';
      if (permission == Permission.camera) {
        infoStr = LocaleKeys.camera.tr;
      } else if (permission == Permission.photos ||
          permission == Permission.storage) {
        infoStr = LocaleKeys.album.tr;
      } else if (permission == Permission.notification) {
        infoStr = LocaleKeys.notification.tr;
      } else if (permission == Permission.location) {
        infoStr = LocaleKeys.location.tr;
      }
      var value = await Get.dialog(_PermissionDialog(infoStr));
      if (value == true && GetPlatform.isAndroid) {
        await openAppSettings();
      }
    }
    return status != PermissionStatus.denied &&
        status != PermissionStatus.permanentlyDenied;
  }
}

class _PermissionDialog extends StatelessWidget {
  const _PermissionDialog(this.permissionName);

  final String permissionName;

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          margin: EdgeInsets.symmetric(horizontal: 27.w),
          padding: EdgeInsets.symmetric(vertical: 24.w, horizontal: 16.w),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(16.w),
            color: AppColor.pageBgColor,
          ),
          child: Column(
            children: [
              Text(
                LocaleKeys.dialogTitleTip.tr,
                style: TextStyle(
                    color: AppColor.textPrimary.withOpacity(0.94),
                    fontSize: 22.sp,
                    fontWeight: FontWeight.w600),
              ),
              8.verticalSpace,
              Text(
                LocaleKeys.goToSettingTip.tr
                    .trArgs([AppConfig.appName, permissionName]),
                textAlign: TextAlign.center,
                style: TextStyle(
                    color: AppColor.textPrimary.withOpacity(0.94),
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w400),
              ),
              32.verticalSpace,
              CommonButton.expend(
                  title: "设置",
                  enable: true,
                  onAsyncClick: () async {
                    Get.back(result: true);
                  }),
              12.verticalSpace,
              CommonButton.expend(
                  title: LocaleKeys.cancel.tr,
                  enable: false,
                  alwaysCanClick: true,
                  onAsyncClick: () async {
                    Get.back();
                    // Get.back(result: true);
                  }),
              // InkWell(
              //   onTap: () {
              //     Get.back();
              //   },
              //   child: Container(
              //     width: double.infinity,
              //     height: 48.w,
              //     alignment: Alignment.center,
              //     decoration: BoxDecoration(
              //         borderRadius: BorderRadius.circular(24.w),
              //         border: Border.all(width: 2, color: AppColor.color1B1B1B)),
              //     child: Text(
              //       "取消",
              //       style: TextStyle(color: AppColor.color1B1B1B, fontSize: 16.sp, fontWeight: FontWeight.w600),
              //     ),
              //   ),
              // )
            ],
          ),
        ),
      ],
    );
  }
}
